package com.bizfx.utils
{
	import com.bizfx.errors.NullArgumentError;
	
//	import org.as3commons.lang.ObjectUtils;
	
	public class ObjectUtils
	{
		public static function setPropertyValue(source:Object, property:String, value:Object):Object {
			if (source == null) {
				return null;
			}
			
			if (StringUtils.isNullOrEmpty(property)) {
				source = value;
			} else {
				var fields:Array = property.split(".");
				
				var temp:Object = source;
				for (var i:int; i < fields.length; i++) {
					var field:String = fields[i];
					if (i == fields.length - 1) {
						temp[field] = value;
					} else {
						if (temp[field] == null) {
							temp[field] = new Object();
						}
						temp = temp[field];
					}
				}
			}
			return source;
		}
		
		public static function getPropertyValue(source:Object, property:String):Object {
			if (source == null) {
				return null;
			}
			
			if (StringUtils.isNullOrEmpty(property)) {
				return source;
			}
			
			var fields:Array = property.split(".");
			var temp:Object = source;
			
			for (var i:int; i < fields.length; i++) {
				var field:String = fields[i];
				if (i == fields.length - 1) {
					if (temp.hasOwnProperty(field)) {
						return temp[field];
					} else {
						return null;
					}
				} else {
					if (temp.hasOwnProperty(field)) {
						temp = temp[field];
					} else {
						return null;
					}
				}
			}
			return null;
		}
		
		public static function callFunction(source:Object, functionName:String, ...args):void {
			var f:Function = source[functionName];
			if (f != null) {
				f.apply(source, args);
			} else {
				throw new Error("方法\"" + functionName + "\"" + "不存在");
			}
		}
	
//		public static function getProperties(object:Object):Array {
//			return org.as3commons.lang.ObjectUtils.getProperties(object);
//		}
	}
}